package com.irun.pm.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.irun.pm.R;
import com.irun.pm.utils.StringUtil;
import com.irun.pm.utils.Utils;

/***
 * @author huangsm
 * @date 2012-5-26
 * @email huangsanm@gmail.com
 * @version 1.0
 * @description 系统登录
 */
public class LoginActivity extends BaseActivity implements OnClickListener {

	private Button mLogin;
	//private Button mCancel;
	private EditText mName;
	private EditText mPwd;
	private CheckBox mRemember;
	
	private SharedPreferences mShared;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		mShared = PreferenceManager.getDefaultSharedPreferences(this);
		mName = (EditText) findViewById(R.id.login_uname);
		mPwd = (EditText) findViewById(R.id.login_pwd);
		
		mLogin = (Button) findViewById(R.id.login);
		//mCancel = (Button) findViewById(R.id.reset);
		
		mRemember = (CheckBox) findViewById(R.id.isRemember);
		
		mLogin.setOnClickListener(this);
		//mCancel.setOnClickListener(this);
		
		//user next login
		if(!StringUtil.isEmpty(mShared.getString(Utils.PREFERENCES_NAME_KEY, "")) 
				&& !StringUtil.isEmpty(mShared.getString(Utils.PREFERENCES_PASSWORD_KEY, ""))){
			Intent login = new Intent(this, PoliceMobileActivity.class);
			startActivityForResult(login, Utils.LOGIN_REQUEST_CODE);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.login:
				if(StringUtil.isEmpty(mName.getText().toString()) || StringUtil.isEmpty(mPwd.getText().toString())){
					Toast.makeText(this, R.string.login_tip, Toast.LENGTH_SHORT).show();
					return;
				}else{
					String name = mName.getText().toString().trim();
					String pwd = mPwd.getText().toString().trim();
					if((Utils.name.equals(name) && Utils.pwd.equals(pwd))
							||(Utils.name1.equals(name) && Utils.pwd1.equals(pwd))
							||(Utils.name2.equals(name) && Utils.pwd2.equals(pwd))
							||(Utils.name3.equals(name) && Utils.pwd3.equals(pwd))
							||(Utils.name4.equals(name) && Utils.pwd4.equals(pwd))){
						if(mRemember.isChecked()){
							mShared.edit()
							.putString(Utils.PREFERENCES_NAME_KEY, name)
							.putString(Utils.PREFERENCES_PASSWORD_KEY, pwd)
							.commit();
						}
						Intent login = new Intent(this, PoliceMobileActivity.class);
						login.putExtra(Utils.PREFERENCES_NAME_KEY, name);
						startActivityForResult(login, Utils.LOGIN_REQUEST_CODE);
					} else {
						Toast.makeText(this, R.string.login_error, Toast.LENGTH_SHORT).show();
						return;
					}
				}
				
				break;
			/*case R.id.reset:
				mName.setText("");
				mPwd.setText("");
				break;*/
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == Utils.LOGIN_REQUEST_CODE){
			if(resultCode == Utils.EXIT_RESULT_CODE){
				finish();
			}
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
			showDialog();
		}
		return false;
	}
	
	private void showDialog(){
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setMessage(R.string.login_msg);
		alert.setTitle(R.string.tip);
		alert.setPositiveButton(R.string.action_ok, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				finish();
			}
		});
		alert.setNegativeButton(R.string.action_cancel, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		alert.show();
	}
}